package leetcode.backtrack;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @author mazouri
 * @create 2021-09-19 22:08
 */
public class _17_letterCombinations {
    //初始对应所有的数字，为了直接对应2-9，新增了两个无效的字符串""
    String[] numString = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
    List<String> res = new LinkedList<>();
    StringBuilder sb = new StringBuilder();
    String digits;

    public List<String> letterCombinations(String digits) {
        if (digits == null || digits.length() == 0) return res;

        this.digits = digits;
        backtrack(0);
        return res;
    }

    private void backtrack(int num) {
        if (sb.length() == digits.length()) {
            res.add(sb.toString());
            return;
        }

        // 23 当前num对应的字母 比如num:0 --> 字符为2
        String s = numString[digits.charAt(num) - '0'];
        for (int i = 0; i < s.length(); i++) {
            sb.append(s.charAt(i));
            backtrack(num+1);
            sb.deleteCharAt(sb.length() - 1);
        }
    }
}
